#include "camprec.h"
#include "LUAScriptPanel.h"
#include "wx/xrc/xmlres.h"          // XRC XML resources
#include "ControlLanguageModelling.h"

CLUAScriptPanel::CLUAScriptPanel (wxWindow* parent, wxWindowID id, const wxPoint& pos, const wxSize& size, long style)
{
	pCP = 0;
	run_status = false;
	bool res = wxXmlResource::Get()->LoadPanel (this, parent,wxT("luascriptpanel"));

	XRCCTRL(*this, "m_luascripttextbox", wxTextCtrl)->Connect( wxEVT_COMMAND_TEXT_ENTER, wxCommandEventHandler (CLUAScriptPanel::OnScriptTexBoxEnter ), NULL, this );
	XRCCTRL(*this, "m_luascripttextbox", wxTextCtrl)->Connect( wxEVT_KEY_DOWN, wxKeyEventHandler (CLUAScriptPanel::OnStatusCtrlKeyTexBoxEnter ), NULL, this );
	Connect(XRCID ("menu_lua_clear"), wxEVT_COMMAND_MENU_SELECTED,	wxCommandEventHandler(CLUAScriptPanel::OnTool_Clear));
	Connect(XRCID ("menu_lua_run"), wxEVT_COMMAND_MENU_SELECTED,	wxCommandEventHandler(CLUAScriptPanel::OnTool_Run));
	Connect(XRCID ("menu_lua_open"), wxEVT_COMMAND_MENU_SELECTED,	wxCommandEventHandler(CLUAScriptPanel::OnTool_Load));
	Connect(XRCID ("m_menu_insert_code_from_combo"), wxEVT_COMMAND_MENU_SELECTED,	wxCommandEventHandler(CLUAScriptPanel::OnTool_RunSavedChank));
}


CLUAScriptPanel::~CLUAScriptPanel(void)
{
}

void CLUAScriptPanel::OnTool_RunSavedChank (wxCommandEvent& event)
{
	wxComboBox* pComboBox = XRCCTRL(*this, "m_comboBoxScriptsChunks", wxComboBox);

	if (!pComboBox)
		return;
	//ArrayStringCode.Add(text);
	int sel = pComboBox->GetSelection () ;
	if (sel < 0)
		return;

	wxString text = pComboBox->GetValue();//ArrayStringCode[sel];
	
	text.Trim();
	if (text != wxEmptyString)
	{
		wxTextCtrl *TextCtrl = XRCCTRL(*this, "m_luascripttextbox", wxTextCtrl);
		TextCtrl->SetValue(text);
	}
}

void CLUAScriptPanel::OnStatusCtrlKeyTexBoxEnter (wxKeyEvent& event)
{
	run_status = false;
	if (event.ShiftDown())
	{
		if (event.GetKeyCode () == VK_RETURN)
		{

				run_status = true;

				wxTextCtrl* pTexBox;

				wxString text;
				if (run_status)
				{
					pTexBox = (wxTextCtrl*)event.GetEventObject ();
					text = pTexBox->GetValue();
					text.Trim(false);
					text.Trim();
					if (pCP)
					{
						pCP->SetText(text);
					}
					if (CControlLanguageModelling::dostring (text.char_str ().data()))
					{
						wxComboBox *comboScripts = XRCCTRL(*this, "m_comboBoxScriptsChunks", wxComboBox);
						if (comboScripts)
						{
							ComboAppend(text);
							//ArrayStringCode.Add(text);
							//comboScripts->Append (text);
							comboScripts->Select (comboScripts->GetCount()-1);
							pTexBox->Clear();
							//event.Skip();
						}
					}
				}
				run_status = false;
				return;
			
		}
		
	}

	event.Skip();
}
void CLUAScriptPanel::OnTool_Clear (wxCommandEvent& event)
{
	//wxToolBar * tb = (wxToolBar*) event.GetEventObject ();
	wxTextCtrl *text = XRCCTRL(*this, "m_luascripttextbox", wxTextCtrl);

	if (text)
	{
		text->Clear();
	}
	
}

void CLUAScriptPanel::OnTool_Run (wxCommandEvent& event)
{
	//wxToolBar * tb = (wxToolBar*) event.GetEventObject ();
	wxTextCtrl *pTexBox = XRCCTRL(*this, "m_luascripttextbox", wxTextCtrl);
	wxString text;
	if (pTexBox)
	{
		text = pTexBox->GetValue();
		if (pCP)
		{
			pCP->SetText(text);
		}
		text.Trim(false);
		text.Trim();
		if (CControlLanguageModelling::dostring (text.char_str ().data()))
		{
			
			wxComboBox *comboScripts = XRCCTRL(*this, "m_comboBoxScriptsChunks", wxComboBox);
			if (comboScripts)
			{
				ComboAppend(text);
				//ArrayStringCode.Add(text);
				//comboScripts->Append (text);
				comboScripts->Select (comboScripts->GetCount()-1);
				pTexBox->Clear();
			}
		}
	}
	
}

void CLUAScriptPanel::ComboAppend (const wxString &s)
{
	wxComboBox *comboScripts = XRCCTRL(*this, "m_comboBoxScriptsChunks", wxComboBox);
	int count = comboScripts->GetCount();
	
	for (int i = 0; i < count; i++)
	{
		wxString val = comboScripts->GetString (i); 
		if (s == val||s.IsEmpty())
			return;
	}
	ArrayStringCode.Add(s);
	comboScripts->AppendString(s);
}

void CLUAScriptPanel::OnTool_Load (wxCommandEvent& event)
{
	 static wxString s_extDef;
	 static wxString s_pathDef = my_wxUtils::GetAppPath(), s_filenameDef;

	//wxToolBar * tb = (wxToolBar*) event.GetEventObject ();
	wxTextCtrl *pTexBox = XRCCTRL(*this, "m_luascripttextbox", wxTextCtrl);
	wxString path;
	wxString text;
	if (pTexBox)
	{
		 path = wxFileSelector(
                        _T("Select the script file to load"),
                        s_pathDef.GetData(), wxEmptyString,
                        s_extDef,
                        wxString::Format
                        (
                            _T("Project (*.cam)|*.cam|Lua Scripts (*.lua)|*.lua|All files (%s)|%s"),
                            wxFileSelectorDefaultWildcardStr,
                            wxFileSelectorDefaultWildcardStr
                        ),
                        wxFD_OPEN|wxFD_CHANGE_DIR|wxFD_PREVIEW,
                        this
                        );
		if (!path.IsEmpty())
		{
			//pTexBox->Clear();
			wxFile file;
			bool res = file.Open (path, wxFile::read);
			if (res)
			{
				long nSize = file.Length();
				char* data_ = new char[nSize];
				memset(data_,0,sizeof(char)*nSize);
				file.Seek(0);
				if (file.Read(data_, nSize)==nSize)
				{
					text = wxString(data_, nSize);
					//text = wxString(data_, wxConvISO8859_1, nSize);
					text.Replace("\r","");
				}
				delete [] data_;

				pTexBox->SetValue(text);
			}
				//pTexBox->LoadFile (path);
		}

	}
	
}
void CLUAScriptPanel::OnScriptTexBoxEnter (wxCommandEvent& event)
{
	//wxTextCtrl* pTexBox;

	//wxString text;
	//if (run_status)
	//{
	//	pTexBox = (wxTextCtrl*)event.GetEventObject ();
	//	text = pTexBox->GetValue();
	//	text.Trim(false);
	//	text.Trim();
	//	if (pCP)
	//	{
	//		pCP->SetText(text);
	//	}
	//	if (CControlLanguageModelling::dostring (text.char_str ().data()))
	//	{
	//		wxComboBox *comboScripts = XRCCTRL(*this, "m_comboBoxScriptsChunks", wxComboBox);
	//		if (comboScripts)
	//		{
	//			comboScripts->Append (text);
	//			comboScripts->Select (comboScripts->GetCount()-1);
	//			pTexBox->Clear();
	//			event.Skip();
	//		}
	//	}
	//}
	//run_status = false;
}